<template>
    <div class="app-container">
        <div class="table-wrapper">
            <h4>供气设施易损性计算</h4>
            <el-form :inline="true" ref="queryRef" :rules="rules" label-position="right" :model="params"
                label-width="200px">
                <el-row justify="center">
                    <el-col :span="11">
                        <el-form-item label="编号：" class="form-item" prop="code">
                            <el-input v-model="params.code" placeholder="" :disabled="true" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="11">
                        <el-form-item label="年代：" class="form-item" prop="year">
                            <el-input v-model="params.year" placeholder="请输入" :disabled="disabled" clearable />
                            <!-- <el-select v-model="params.years" placeholder="请选择" class="form-select" :disabled="disabled"
                                clearable>
                                <el-option v-for="item in yearsOptions" :key="item.value" :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select> -->
                        </el-form-item>
                    </el-col>
                    <!-- <el-col :span="11">
                        <el-form-item label="维护人：" class="form-item" prop="createUserName">
                            <el-input v-model="params.createUserName" placeholder="" clearable />
                        </el-form-item>
                    </el-col> -->
                </el-row>
                <!-- <el-row justify="center">
                    <el-col :span="11">
                        <el-form-item label="维护人所属单位：" class="form-item" prop="orgName">
                            <el-input v-model="params.orgName" placeholder="" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="11">
                        <el-form-item label="维护时间:" class="form-item" prop="createTime">
                            <el-date-picker v-model="params.createTime" type="date" placeholder="选择日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row> -->
                <el-row justify="center">
                    <el-col :span="11">
                        <el-form-item label="设防情况：" class="form-item" prop="fortification">
                            <el-select v-model="params.fortification" placeholder="请选择" class="form-select"
                                :disabled="disabled" clearable>
                                <el-option v-for="item in optionList1" :label="item.dictItemName"
                                    :value="item.dictItemCode">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="11"></el-col>
                </el-row>
                <el-row v-if="isAdd" justify="end">
                    <el-form-item class="bottom-button">
                        <!-- <el-button type="primary" @click="showUploadDialog = true">参数导入</el-button> -->
                        <el-button type="primary" @click="handleVerify" v-if="!route.query.id">验证</el-button>
                        <el-button type="primary" @click="onCalc" v-if="route.query.id">计算</el-button>
                        <el-button type="primary" @click="onSubmit" v-if="route.query.id">提交</el-button>
                    </el-form-item>
                </el-row>
            </el-form>
            <!-- 数据导入弹框 -->
            <import-data v-if="showUploadDialog" v-model="showUploadDialog" ref="uploadDialog" :uploadUrl="uploadUrl"
                downloadFileName="供气设施易损性计算参数导入.xlsx" @importSuccess="onImport"></import-data>

            <el-form :inline="true" ref="tableRef" :rules="rules" label-position="right" :model="tableFormData">
                <h4 class="table-title">导轨发生塑性弯曲时所能承受的剪力为F1：</h4>
                <!-- <el-row justify="end" class="table-tools-btn">
                    <el-form-item class="bottom-button">
                        <el-button @click="onCalcF1()" type="primary">计算</el-button>
                    </el-form-item>
                </el-row> -->
                <el-table class="listTables" ref="multipleTableRef" border :data="tableFormData.tableData"
                    style="width: 100%" @selection-change="handleSelectionChange"
                    :cell-style="{ border: '1px solid #e5ebf7' }"
                    :header-cell-style="{ border: '1px solid #e5ebf7', borderLeft: 'none' }">
                    <el-table-column type="selection" width="55" label="序号" />
                    <el-table-column header-align="center" align="center">
                        <template #header>
                            <span class="color-red">b</span>导轨塑性弯曲长度 (m)
                        </template>
                        <template #default="scope">
                            <el-form-item :prop="`tableData.${scope.$index}.railLength`" :rules="rules.railLength">
                                <el-input v-model="scope.row.railLength" :disabled="scope.row.disabled" size="small"
                                    type="number"></el-input>
                            </el-form-item>
                        </template>
                    </el-table-column>
                    <el-table-column header-align="center" align="center">
                        <template #header>
                            <span class="color-red">h</span>导轨中腹厚度 (m)
                        </template>
                        <template #default="scope">
                            <el-form-item :prop="`tableData.${scope.$index}.thickness1`" :rules="rules.thickness1">
                                <el-input v-model="scope.row.thickness1" :disabled="scope.row.disabled" size="small"
                                    type="number"></el-input>
                            </el-form-item>
                        </template>
                    </el-table-column>
                    <el-table-column header-align="center" align="center">
                        <template #header>
                            <span class="color-red">σs</span>导轨钢材屈服应力（N/㎡）
                        </template>
                        <template #default="scope">
                            <el-form-item :prop="`tableData.${scope.$index}.yieldStress`" :rules="rules.yieldStress">
                                <el-input v-model="scope.row.yieldStress" :disabled="scope.row.disabled" size="small"
                                    type="number"></el-input>
                            </el-form-item>
                        </template>
                    </el-table-column>
                    <el-table-column header-align="center" align="center">
                        <template #header>
                            <span class="color-red">H</span> 导轨高度 (m)
                        </template>
                        <template #default="scope">
                            <el-form-item :prop="`tableData.${scope.$index}.railHeight`" :rules="rules.railHeight">
                                <el-input v-model="scope.row.railHeight" :disabled="scope.row.disabled" size="small"
                                    type="number"></el-input>
                            </el-form-item>
                        </template>
                    </el-table-column>
                    <el-table-column prop="result1" header-align="center" align="center" label="公式">
                        <template #header>
                            <Formula tagOffset="-6px" fontWeight="bold" :option="[
                                { content: 'F1' },
                                { content: '=', margin: '0 8px' },
                                { content: 'bh', super: '2' },
                                { content: '(' },
                                { content: 'σ', sub: 's' },
                                { content: ')' },
                                { content: '/' },
                                { content: '(' },
                                { content: '4H' },
                                { content: ')' },
                            ]"></Formula>
                        </template>
                    </el-table-column>
                    <el-table-column header-align="center" align="center" label="修正值">
                        <template #default="scope">
                            <el-form-item :prop="`tableData.${scope.$index}.result1Correct`" :rules="rules.result1Correct">
                                <el-input v-model="scope.row.result1Correct" :disabled="scope.row.disabled" size="small"
                                    type="number"></el-input>
                            </el-form-item>
                        </template>
                    </el-table-column>
                </el-table>

                <h4 class="table-title">导轨焊缝抗剪承载力为F2：</h4>
                <el-table class="listTables" ref="multipleTableRef" border :data="tableFormData.tableData"
                    style="width: 100%" @selection-change="handleSelectionChange"
                    :cell-style="{ border: '1px solid #e5ebf7' }"
                    :header-cell-style="{ border: '1px solid #e5ebf7', borderLeft: 'none' }">
                    <el-table-column type="selection" width="55" label="序号" />
                    <el-table-column header-align="center" align="center">
                        <template #header>
                            <span class="color-red">b</span>导轨焊缝宽度(m)
                        </template>
                        <template #default="scope">
                            <el-form-item :prop="`tableData.${scope.$index}.weldWidth`" :rules="rules.weldWidth">
                                <el-input v-model="scope.row.weldWidth" :disabled="scope.row.disabled" size="small"
                                    type="number"></el-input>
                            </el-form-item>
                        </template>
                    </el-table-column>
                    <el-table-column header-align="center" align="center" label="h1 导轨焊缝高度(m)">
                        <template #header>
                            <span class="color-red">h1</span>导轨焊缝高度(m)
                        </template>
                        <template #default="scope">
                            <el-form-item :prop="`tableData.${scope.$index}.weldHeight`" :rules="rules.weldHeight">
                                <el-input v-model="scope.row.weldHeight" :disabled="scope.row.disabled" size="small"
                                    type="number"></el-input>
                            </el-form-item>
                        </template>
                    </el-table-column>
                    <el-table-column header-align="center" align="center" label="[τ]允许焊接应力（N/㎡）">
                        <template #default="scope">
                            <el-form-item :prop="`tableData.${scope.$index}.weldStress`" :rules="rules.weldStress">
                                <el-input v-model="scope.row.weldStress" :disabled="scope.row.disabled" size="small"
                                    type="number"></el-input>
                            </el-form-item>
                        </template>
                    </el-table-column>
                    <el-table-column header-align="center" align="center">
                        <template #header>
                            <span class="color-red">H</span>破坏焊接长度 (m)
                        </template>
                        <template #default="scope">
                            <el-form-item :prop="`tableData.${scope.$index}.damagedWeldLength`"
                                :rules="rules.damagedWeldLength">
                                <el-input v-model="scope.row.damagedWeldLength" :disabled="scope.row.disabled" size="small"
                                    type="number"></el-input>
                            </el-form-item>
                        </template>
                    </el-table-column>
                    <el-table-column prop="result2" header-align="center" align="center">
                        <template #header>
                            <Formula tagOffset="-6px" fontWeight="bold" :option="[
                                { content: 'F2' },
                                { content: '=', margin: '0 8px' },
                                { content: '2bh', sub: '1' },
                                { content: '[' },
                                { content: 'τ' },
                                { content: ']' },
                                { content: 'H' },
                            ]"></Formula>
                        </template>
                    </el-table-column>
                    <el-table-column header-align="center" align="center" label="修正值">
                        <template #default="scope">
                            <el-form-item :prop="`tableData.${scope.$index}.result2Correct`" :rules="rules.result2Correct">
                                <el-input v-model="scope.row.result2Correct" :disabled="scope.row.disabled" size="small"
                                    type="number"></el-input>
                            </el-form-item>
                        </template>
                    </el-table-column>
                </el-table>

                <h4 class="table-title">导轮轴抗弯曲承载力为F3：</h4>
                <!-- <el-row justify="end" class="table-tools-btn">
                    <el-form-item class="bottom-button">
                        <el-button @click="onCalcF3()" type="primary">计算</el-button>
                    </el-form-item>
                </el-row> -->
                <el-table class="listTables" ref="multipleTableRef" border :data="tableFormData.tableData"
                    style="width: 100%" @selection-change="handleSelectionChange"
                    :cell-style="{ border: '1px solid #e5ebf7' }"
                    :header-cell-style="{ border: '1px solid #e5ebf7', borderLeft: 'none' }">
                    <el-table-column type="selection" width="55" label="序号" />
                    <el-table-column header-align="center" align="center">
                        <template #header>
                            <span class="color-red">r</span>导轮轴半径(m)
                        </template>
                        <template #default="scope">
                            <el-form-item :prop="`tableData.${scope.$index}.axisRadius`" :rules="rules.axisRadius">
                                <el-input v-model="scope.row.axisRadius" :disabled="scope.row.disabled" size="small"
                                    type="number"></el-input>
                            </el-form-item>
                        </template>
                    </el-table-column>
                    <el-table-column header-align="center" align="center" label="[σs]导轮轴屈服应力">
                        <template #default="scope">
                            <el-form-item :prop="`tableData.${scope.$index}.yieldStress1`" :rules="rules.yieldStress1">
                                <el-input v-model="scope.row.yieldStress1" :disabled="scope.row.disabled" size="small"
                                    type="number"></el-input>
                            </el-form-item>
                        </template>
                    </el-table-column>
                    <el-table-column header-align="center" align="center" label="ξ极限能力系数">
                        <template #default="scope">
                            <el-form-item :prop="`tableData.${scope.$index}.ultimateCapacity`"
                                :rules="rules.ultimateCapacity">
                                <el-input v-model="scope.row.ultimateCapacity" :disabled="scope.row.disabled" size="small"
                                    type="number"></el-input>
                            </el-form-item>
                        </template>
                    </el-table-column>
                    <el-table-column header-align="center" align="center" label="">
                        <template #header>
                            <span class="color-red">L</span>导轮轴计算长度 (m)
                        </template>
                        <template #default="scope">
                            <el-form-item :prop="`tableData.${scope.$index}.calculatedLength`"
                                :rules="rules.calculatedLength">
                                <el-input v-model="scope.row.calculatedLength" :disabled="scope.row.disabled" size="small"
                                    type="number"></el-input>
                            </el-form-item>
                        </template>
                    </el-table-column>
                    <el-table-column prop="result3" header-align="center" align="center">
                        <template #header>
                            <Formula tagOffset="-6px" fontWeight="bold" :option="[
                                { content: 'F3' },
                                { content: '=', margin: '0 8px' },
                                { content: 'πr', super: '3' },
                                { content: 'ξ' },
                                { content: '[' },
                                { content: 'σ', sub: 's' },
                                { content: ']' },
                                { content: '/' },
                                { content: '(' },
                                { content: '4L' },
                                { content: ')' },
                            ]"></Formula>
                        </template>
                    </el-table-column>
                    <el-table-column header-align="center" align="center" label="修正值">
                        <template #default="scope">
                            <el-form-item :prop="`tableData.${scope.$index}.result3Correct`" :rules="rules.result3Correct">
                                <el-input v-model="scope.row.result3Correct" :disabled="scope.row.disabled" size="small"
                                    type="number"></el-input>
                            </el-form-item>
                        </template>
                    </el-table-column>
                </el-table>

                <h4 class="table-title">导轮轴支承架脱焊抗剪承载力F4：</h4>
                <el-table class="listTables" ref="multipleTableRef" border :data="tableFormData.tableData"
                    style="width: 100%" @selection-change="handleSelectionChange"
                    :cell-style="{ border: '1px solid #e5ebf7' }"
                    :header-cell-style="{ border: '1px solid #e5ebf7', borderLeft: 'none' }">
                    <el-table-column type="selection" width="55" label="序号" />
                    <el-table-column header-align="center" align="center">
                        <template #header>
                            <span class="color-red">L</span>底板长度
                        </template>
                        <template #default="scope">
                            <el-form-item :prop="`tableData.${scope.$index}.bottomPlateLength`"
                                :rules="rules.bottomPlateLength">
                                <el-input v-model="scope.row.bottomPlateLength" :disabled="scope.row.disabled" size="small"
                                    type="number"></el-input>
                            </el-form-item>
                        </template>
                    </el-table-column>
                    <el-table-column header-align="center" align="center">
                        <template #header>
                            <span class="color-red">b</span>焊缝宽度(m)
                        </template>
                        <template #default="scope">
                            <el-form-item :prop="`tableData.${scope.$index}.weldingWidth`" :rules="rules.weldingWidth">
                                <el-input v-model="scope.row.weldingWidth" :disabled="scope.row.disabled" size="small"
                                    type="number"></el-input>
                            </el-form-item>
                        </template>
                    </el-table-column>
                    <el-table-column header-align="center" align="center" label="[τ]焊接允许应力（N/㎡）">
                        <template #default="scope">
                            <el-form-item :prop="`tableData.${scope.$index}.weldStress`" :rules="rules.weldStress">
                                <el-input v-model="scope.row.weldStress" :disabled="scope.row.disabled" size="small"
                                    type="number"></el-input>
                            </el-form-item>
                        </template>
                    </el-table-column>
                    <el-table-column prop="result4" header-align="center" align="center">
                        <template #header>
                            <Formula tagOffset="-6px" fontWeight="bold" :option="[
                                { content: 'F4' },
                                { content: '=', margin: '0 8px' },
                                { content: '2Lb[τ]' },
                            ]"></Formula>
                        </template>
                    </el-table-column>
                    <el-table-column header-align="center" align="center" label="修正值">
                        <template #default="scope">
                            <el-form-item :prop="`tableData.${scope.$index}.result4Correct`" :rules="rules.result4Correct">
                                <el-input v-model="scope.row.result4Correct" :disabled="scope.row.disabled" size="small"
                                    type="number"></el-input>
                            </el-form-item>
                        </template>
                    </el-table-column>
                </el-table>
            </el-form>

            <!-- 版本查看 -->
            <GetVersion v-if="openDialog" v-model:open="openDialog" @recalculateVersion="recalculateVersion" type="3" />
            <el-dialog v-model="dialogVisible" @close="cancle" title="提示" width="500px">
                <p v-for="item in messageList">{{ item.label }}
                    <el-popover placement="bottom" title="提示详情" :width="200" trigger="click">
                        <template #reference>
                            <span style="color: #107ce5;cursor: pointer;">提示详情</span>
                        </template>
                        <template #default>
                            <p v-for="val in item.value">{{ val }}</p>
                        </template>
                    </el-popover>
                </p>
                <p>&nbsp;</p>
            </el-dialog>
        </div>
    </div>
</template>

<script setup>
import { reactive, toRefs } from "vue";
import { ElMessage, ElMessageBox } from 'element-plus'
// 接口
import { getInfo } from '@/api/login';
import {
    getByIdAndType,//查询
    calculationCrsForce,// 计算
    saveSgManagementResult,//保存
    importCsfBearingUrl, //参数导入
    recalculateSg,//验证
    saveSgResult,//重新计算
} from "@/api/lifelineVulnerAnalysis/lifelineVulnerAnalysis.js";
import { getValidDictItemsByDictCode } from "@/api/map/disasterDataManagement/lifelineSystemInfo.js";
// 公用方法
import { parseTime } from "@/utils/ruoyi.js";
// 引入公式组件
import Formula from '@/components/Formula';
// import MultilineFormula from '@/components/Formula/multiline';
import GetVersion from '../underPipeNet/dataFile/Version.vue';
// 引入导入弹框
import ImportData from "@/components/ImportData";

// 引入store
import useUserStore from '@/store/modules/user';
import useDictStore from '@/store/modules/dict';
// 获取pinia
const userStore = useUserStore();
const dictStore = useDictStore();
// 用户名
const userName = computed(() => userStore.name || '');
// 所属机构名称
const orgName = computed(() => userStore.orgName || '');

// 当前实例
const { proxy } = getCurrentInstance();
// 当前路由
const route = useRoute();
const router = useRouter();

// 打开类型，query为查看
let operateType = computed(() => route.query.operateType || false);
// 是否是新增页，有路由参数是详情页，否则是新增页
let isAdd = computed(() => (route.query.id && route.query.operateType ? false : true));
// 禁用
const disabled = ref(false);

//最多保留N小数 ,N 在 options {decimals: N} 中定义
const positivedecimals = (rule, value, callback) => {
    value = String(value);
    if (
        value.indexOf(".") != -1 &&
        (value.split(".").length > rule.options.decimals ||
            value.split(".")[0] === "" ||
            value.split(".")[1] === "")
    ) {
        callback(new Error()); //防止输入多个小数点
    } else if (
        value.indexOf(".") != -1 &&
        value.split(".")[1].length > rule.options.decimals
    ) {
        callback(new Error()); //小数点后两位
    } else {
        callback();
    }
};
//验证是否yyyy年份（非必填）
const isYearNotMust = (rule, value, callback) => {
    if (value == '' || value == undefined || value == null) {
        callback();
    } else if (isNaN(Number(value))) {
        callback(new Error('请输入yyyy格式如2022'));
    } else if (value.toString().length != 4) {
        callback(new Error('请输入yyyy格式如2022'));
    } else if (value.toString().indexOf(".") != -1) {
        callback(new Error('请输入yyyy格式如2022'));
    } else {
        callback();
    }
}
const data = reactive({
    // 表单参数
    params: {
        code: "",//编号
        rebarType: "",//钢筋类型
        year: "",//年代
        fortification: "", //设防情况
        createTime: parseTime(new Date()), //维护时间
        createUserName: userName.value, //维护人
        orgName: orgName.value, //维护人所属单位
    },
    // 表单验证规则
    rules: {
        fortification: [
            { required: true, message: '请选择', trigger: 'change' }
        ],
        rebarType: [
            { required: true, message: '请选择', trigger: 'change' }
        ],
        year: [
            {
                required: true,
                message: "请输入正确数值",
                validator: isYearNotMust,
                trigger: ["change", "blur"],
            }
        ],
        railLength: [
            { required: true, message: "请输入", trigger: "blur" },
            {
                message: "请输入正确数值，最多保留四位小数",
                validator: positivedecimals,
                options: { decimals: 4 },
                trigger: ["change", "blur"],
            },
        ],
        thickness1: [
            { required: true, message: "请输入", trigger: "blur" },
            {
                message: "请输入正确数值，最多保留四位小数",
                validator: positivedecimals,
                options: { decimals: 4 },
                trigger: ["change", "blur"],
            },
        ],
        yieldStress: [
            { required: true, message: "请输入", trigger: "blur" },
            {
                message: "请输入正确数值，最多保留四位小数",
                validator: positivedecimals,
                options: { decimals: 4 },
                trigger: ["change", "blur"],
            },
        ],
        railHeight: [
            { required: true, message: "请输入", trigger: "blur" },
            {
                message: "请输入正确数值，最多保留四位小数",
                validator: positivedecimals,
                options: { decimals: 4 },
                trigger: ["change", "blur"],
            },
        ],
        weldWidth: [
            { required: false, message: "", trigger: "blur" },
            {
                message: "请输入正确数值，最多保留四位小数",
                validator: positivedecimals,
                options: { decimals: 4 },
                trigger: ["change", "blur"],
            },
        ],
        weldHeight: [
            { required: false, message: "", trigger: "blur" },
            {
                message: "请输入正确数值，最多保留四位小数",
                validator: positivedecimals,
                options: { decimals: 4 },
                trigger: ["change", "blur"],
            },
        ],
        weldStress: [
            { required: true, message: "请输入", trigger: "blur" },
            {
                message: "请输入正确数值，最多保留四位小数",
                validator: positivedecimals,
                options: { decimals: 4 },
                trigger: ["change", "blur"],
            },
        ],
        damagedWeldLength: [
            { required: true, message: "请输入", trigger: "blur" },
            {
                message: "请输入正确数值，最多保留四位小数",
                validator: positivedecimals,
                options: { decimals: 4 },
                trigger: ["change", "blur"],
            },
        ],
        axisRadius: [
            { required: true, message: "请输入", trigger: "blur" },
            {
                message: "请输入正确数值，最多保留四位小数",
                validator: positivedecimals,
                options: { decimals: 4 },
                trigger: ["change", "blur"],
            },
        ],
        yieldStress1: [
            { required: true, message: "请输入", trigger: "blur" },
            {
                message: "请输入正确数值，最多保留四位小数",
                validator: positivedecimals,
                options: { decimals: 4 },
                trigger: ["change", "blur"],
            },
        ],
        ultimateCapacity: [
            { required: false, message: "", trigger: "blur" },
            {
                message: "请输入正确数值，最多保留四位小数",
                validator: positivedecimals,
                options: { decimals: 4 },
                trigger: ["change", "blur"],
            },
        ],
        calculatedLength: [
            { required: false, message: "", trigger: "blur" },
            {
                message: "请输入正确数值，最多保留四位小数",
                validator: positivedecimals,
                options: { decimals: 4 },
                trigger: ["change", "blur"],
            },
        ],
        bottomPlateLength: [
            { required: true, message: "请输入", trigger: "blur" },
            {
                message: "请输入正确数值，最多保留四位小数",
                validator: positivedecimals,
                options: { decimals: 4 },
                trigger: ["change", "blur"],
            },
        ],
        weldingWidth: [
            { required: true, message: "请输入", trigger: "blur" },
            {
                message: "请输入正确数值，最多保留四位小数",
                validator: positivedecimals,
                options: { decimals: 4 },
                trigger: ["change", "blur"],
            },
        ],
        weldStress: [
            { required: false, message: "", trigger: "blur" },
            {
                message: "请输入正确数值，最多保留四位小数",
                validator: positivedecimals,
                options: { decimals: 4 },
                trigger: ["change", "blur"],
            },
        ],
        destroyWeldLength: [
            { required: true, message: "请输入", trigger: "blur" },
            {
                message: "请输入正确数值，最多保留四位小数",
                validator: positivedecimals,
                options: { decimals: 4 },
                trigger: ["change", "blur"],
            },
        ],
        // result1Correct: [
        //     { required: true, message: "请输入", trigger: "blur" },
        //     {
        //         message: "请输入正确数值，最多保留四位小数",
        //         validator: positivedecimals,
        //         options: { decimals: 4 },
        //         trigger: ["change", "blur"],
        //     },
        // ],
        // result2Correct: [
        //     { required: true, message: "请输入", trigger: "blur" },
        //     {
        //         message: "请输入正确数值，最多保留四位小数",
        //         validator: positivedecimals,
        //         options: { decimals: 4 },
        //         trigger: ["change", "blur"],
        //     },
        // ],
        // result3Correct: [
        //     { required: false, message: "", trigger: "blur" },
        //     {
        //         message: "请输入正确数值，最多保留四位小数",
        //         validator: positivedecimals,
        //         options: { decimals: 4 },
        //         trigger: ["change", "blur"],
        //     },
        // ],
        // result4Correct: [
        //     { required: true, message: "请输入", trigger: "blur" },
        //     {
        //         message: "请输入正确数值，最多保留四位小数",
        //         validator: positivedecimals,
        //         options: { decimals: 4 },
        //         trigger: ["change", "blur"],
        //     },
        // ],
    },
    // 设防情况
    fortificationOptions: dictStore.fortificationOptions || [],
    // 年份
    yearsOptions: dictStore.yearsOptions1 || [],
    // 表格
    tableFormData: {
        tableData: [],
    },
});
const {
    params,
    rules,
    fortificationOptions,
    yearsOptions,
    tableFormData,
} = toRefs(data);

//表格
const multipleTableRef = ref();
const handleSelectionChange = (val) => {
    multipleSelection.value = val;
};

const onCalc = () => {
    proxy.$refs['queryRef'].validate((valid, fields) => {
        if (valid) {
            let formData = tableFormData.value.tableData[0];
            let query = {
                id: formData.id,
                railLength: formData.railLength,
                railHeight: formData.railHeight,
                thickness1: formData.thickness1,
                weldWidth: formData.weldWidth,
                weldHeight: formData.weldHeight,
                weldStress: formData.weldStress,
                damagedWeldLength: formData.damagedWeldLength,
                axisRadius: formData.axisRadius,
                yieldStress1: formData.yieldStress1,
                ultimateCapacity: formData.ultimateCapacity,
                calculatedLength: formData.calculatedLength,
                bottomPlateLength: formData.bottomPlateLength,
                weldingWidth: formData.weldingWidth,
                result1Correct: formData.result1Correct,
                result2Correct: formData.result2Correct,
                result3Correct: formData.result3Correct,
                result4Correct: formData.result4Correct,
                yieldStress: formData.yieldStress,
                result1: formData.result1,
                result2: formData.result2,
                result3: formData.result3,
                result4: formData.result4,
            }
            proxy.$modal.loading('正在努力计算,请耐心等待！');
            calculationCrsForce(query).then((res) => {
                proxy.$modal.closeLoading();
                const { code, data } = res;
                if (code === 200 && data) {
                    // 表格数据回显
                    tableFormData.value.tableData = [{ ...tableFormData.value.tableData[0], ...data }];
                    proxy.$modal.msgSuccess('计算完成！计算结果未保存，若需保存结果，请计算后点击提交按钮！');
                }
            }).catch(err => {
                proxy.$modal.closeLoading();
                console.log(err.message);
            });
        } else {
            proxy.$refs['queryRef'].scrollToField(Object.keys(fields)[0]);
        }
    })
};

// 导入弹框是否显示
let showUploadDialog = ref(false);
// 导入数据接口路径
const uploadUrl = ref(importCsfBearingUrl);
// 导入成功回调
const onImport = (response, uploadFile, uploadFiles) => {
    const { code, data } = response;
    if (code == 200 && data) {
        proxy.$modal.msgSuccess('导入数据成功!');
        const { crbList, crwList, csfList, cssList } = data;
        // 列表数据回显
        if (crbList) {
            tableFormData.value.tableData = [...crbList];
        }
        if (crwList) {
            tableFormData.value.tableData = [...crwList];
        }
        if (csfList) {
            tableFormData.value.tableData = [...csfList];
        }
        if (cssList) {
            tableFormData.value.tableData = [...cssList];
        }
    }
}
//供气设施易损性计算结果保存
const onSubmit = () => {
    proxy.$refs['queryRef'].validate(async (valid, fields) => {
        if (valid) {
            // 表格表单验证
            const tableValidate = await proxy.$refs['tableRef'].validate((valid, fields) => { valid, fields });
            if (tableValidate) {
                let formData = tableFormData.value.tableData[0];
                let query = {
                    type: '3',
                    airSupplyParam: {
                        id: formData.id,
                        railLength: formData.railLength,
                        railHeight: formData.railHeight,
                        thickness1: formData.thickness1,
                        weldWidth: formData.weldWidth,
                        weldHeight: formData.weldHeight,
                        weldStress: formData.weldStress,
                        damagedWeldLength: formData.damagedWeldLength,
                        axisRadius: formData.axisRadius,
                        yieldStress1: formData.yieldStress1,
                        ultimateCapacity: formData.ultimateCapacity,
                        calculatedLength: formData.calculatedLength,
                        bottomPlateLength: formData.bottomPlateLength,
                        weldingWidth: formData.weldingWidth,
                        result1Correct: formData.result1Correct,
                        result2Correct: formData.result2Correct,
                        result3Correct: formData.result3Correct,
                        result4Correct: formData.result4Correct,
                        yieldStress: formData.yieldStress,
                        result1: formData.result1,
                        result2: formData.result2,
                        result3: formData.result3,
                        result4: formData.result4,
                    }
                }
                proxy.$modal.loading('正在提交,请耐心等待！');
                saveSgManagementResult(query).then((res) => {
                    proxy.$modal.closeLoading();
                    const { code } = res;
                    if (code === 200) {
                        proxy.$modal.msgSuccess("保存成功！");
                        tableFormData.value.tableData = [{ ...tableFormData.value.tableData[0], ...data }];
                        // 延迟跳转
                        setTimeout(() => {
                            router.push('/lifelineVulnerAnalysis/lifelineMain')
                        }, 1500)
                    } else {
                        proxy.$modal.msgError("保存失败！");
                    }
                }).catch(err => {
                    proxy.$modal.closeLoading();
                    console.log(err.message);
                });
            }
        } else {
            proxy.$refs['queryRef'].scrollToField(Object.keys(fields)[0])
        }
    })
}

// 显示弹框
const openDialog = ref(false)
const handleVerify = () => {
    openDialog.value = true;
}
// 验证
const messageList = ref([]);
const dialogVisible = ref(false);
const cancle = () => {
    dialogVisible.value = false;
}
const recalculateVersion = (version) => {
    openDialog.value = false;
    recalculateSg({ versionId:version.id, type: '3' }).then(res => {
        if (JSON.stringify(res.data) == '{}') {
            ElMessageBox.confirm(
                '数据通过验证，是否计算？', '提示',
                {
                    confirmButtonText: '计算并保存',
                    cancelButtonText: '取消',
                    type: 'success',
                }
            )
                .then(() => {
                    saveSgResult({ versionId:version.id }).then(response => {
                        if (response.code == 200) {
                            router.push(`/lifelineVulnerAnalysis/lifelineMain?type=3`)
                        }
                    })
                }).catch(() => { })
        } else {
            const { data } = res;
            messageList.value = [];
            for (let i in data) {
                messageList.value.push({ label: `第${i}行计算参数未填写完整，请补充完整再计算。`, value: data[i] })
            }
            dialogVisible.value = true;
        }
    })
}

// 获取数据
function getData(query) {
    getByIdAndType(query).then((res) => {
        const { code, data } = res;
        if (code === 200 && data) {
            const { airReceiverVo, tableEntity } = data;
            if (airReceiverVo) {
                // 基础信息
                params.value.code = tableEntity.code;
                params.value.fortification = tableEntity.fortification;
                params.value.year = tableEntity.year;
                // 维护时间
                const dateTime = new Date()
                params.value.createTime = parseTime(dateTime);
                // 根据id获取用户信息
                params.value.createUserName = userStore.name;
                params.value.orgName = userStore.orgName;
                // getUserInfo(tableEntity.createUser);
                //循环遍历之前加上非空判断
                tableFormData.value.tableData = [{ ...airReceiverVo, disabled: operateType.value === 'query' ? true : false }]
            }
        }
    }).catch(err => {
        console.log(err.message);
    });
}

//字典表查询
const optionList1 = ref([])
/** 获取字典值 */
async function getDictData() {
    await getValidDictItemsByDictCode("smxsfqk").then(response => {
        optionList1.value = response.data;//设防情况
    });

}

getDictData();
// 挂载
onMounted(() => {
    const id = route.query.id;
    if (id) {
        disabled.value = true;
        // 获取数据
        getData({ id, type: 3 });
    } else {
        disabled.value = false;
    }
});
</script>

<style lang="scss" scoped>
.table-wrapper {
    position: relative;

    h4 {
        font-size: 16px;
        font-weight: bold;
        color: #107ce5;
        text-align: center;
    }

    // 字体红色
    .color-red {
        color: red !important;
    }

    // 表格标题
    .table-title {
        margin-top: 50px;
    }

    // 表格工具按钮组
    .table-tools-btn {
        margin-top: -50px;
        float: right;
    }
}
</style>


